var grid;
var opeForm;

$(function() {
	opeForm = $('#opeForm');
	//$('input[type="button"]').button();

	grid = $('#gridTable').jqGrid({
		caption : 'Certificate Association List',
		datatype : 'local',
		mtype : 'post',
		height : '400',
		autowidth : true,
		colModel : [ {
			name : 'CER_TYPE',
			label : 'Certificate type',
			sortable:false
		}, {
			name : 'STATUS',
			label : 'Status',
			sortable:false
		}, {
			name : 'Action',
			label : 'Action',
			formatter: function(cellvalue, options, rowObject){
				var status = rowObject.STATUS;
				if (status == null) {
					var str = '<a style="cursor:pointer; color:blue" onclick=\'showChooseResourceOfCer("' + rowObject.CER_TYPE + '")\'>Register</a>';
					return str;
				}
				
				else if (status == 'Pending approval') {
					var str = '<a style="cursor:pointer; color:blue" onclick=\'viewOperation("' + rowObject.CN + '")\'>View</a>';
					return str;
				} 
				
				else if (status == 'Pending confirm') {
					var str = '<a style="cursor:pointer; color:blue" onclick=\'viewOperation("' + rowObject.CN + '")\'>View</a>';
					var str1 = '<a style="cursor:pointer; color:blue" onclick=\'showConfirmRegister("' + rowObject.CN + '")\'>Confirm</a>';
					return str + '&nbsp;&nbsp;&nbsp;&nbsp;' + str1;
				}
				
				else if (status == 'Activated' || status == 'Dectivated') {
					var str = '<a style="cursor:pointer; color:blue" onclick=\'viewOperation("' + rowObject.CN + '")\'>View</a>';
					return str;
				} 
				
				else {
					return "undefined type";
				}
				
				
			},
			sortable:false
		} ],
		viewrecords : true,
		multiselect : false,
		rownumbers: true,
		rownumWidth: 40,
		rowNum: '-1',	// disable paging
		prmNames : {
			page : 'pageNum', // 表示请求页码的参数名称
			rows : 'pageSize' // 表示请求行数的参数名称
		},
		jsonReader : {
			root : 'rows', // json中代表实际模型数据的入口
			page : 'pageNum', // json中代表当前页码的数据
			total : 'totalPage', // json中代表页码总数的数据
			records : 'totalNum', // json中代表数据行总数的数据
			repeatitems : false
		}
	});

	queryOperation();
	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);

});

function queryOperation() {
	var params = opeForm.serializeObject();
	grid.jqGrid('setGridParam', {
		url : 'BD_USER_REG!queryList.do',
		datatype : 'json',
		postData : jQuery.extend({}, params)
	}).trigger('reloadGrid');
}

function clearOperation() {
	opeForm.resetForm();
}

function viewOperation(param) {
	if (param == null) {
		alert('Can not get mandatory attr!');
		return;
	}
	var url = 'BD_USER_REG!showInfo.do?model.CN=' + param;
	openWin(url, 'Detail page');
	
}

function showChooseResourceOfCer(cerType) {
	if (cerType == null) {
		alert('Can not get certificate type!');
		return;
	}
	var url = 'BD_USER_REG!showChooseResourceOfCer.do?operationType=6&model.CER_TYPE=' + cerType;
	openWin(url, 'Detail page');
	
}

function showConfirmRegister(cn) {
	if (cn == null) {
		alert('Can not get mandatory attr!');
		return;
	}
	var url = 'BD_USER_REG!showChooseResourceOfCer.do?operationType=7&model.CN=' + cn;
	openWin(url, 'Detail page');
	
}

function afterSave() {
	queryOperation();
}
